﻿/*
 * File: TwoDeeGamePlayBaseScene_Helper.cs
 * Author: Gourav Das
 * Purpose: Declaraton and helper methods for 2D gameplay
 * Created: May31 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    partial class GamePlayBaseScene : Scene
    {
        #region Variable Declaration

        BaseCamera staticCamera;
        BirdEyeViewBallChaseCamera birdEyeViewChaseCamera;

        Viewport NormalViewPort = new Viewport();
        Viewport RtViewPort = new Viewport((int)((float)280 * Game22Yards.scaleFactor.widthScale), (int)((float)90 * Game22Yards.scaleFactor.htScale), (int)((float)1070 * Game22Yards.scaleFactor.widthScale), (int)((float)580 * Game22Yards.scaleFactor.widthScale));

        bool bShowMobile = true;
        bool bMobileAppear = false;
        bool bMobileDissappear = false;
        bool bShowField = false;
        float TransitionValue = 0.0f;

        private Texture2D TwoDeeBall;
        private RenderTarget2D RenderTarget1;
        private RenderTarget2D RenderTarget2;
        private RenderTarget2D RenderTarget3;
        Texture2D OriginalViewPortTexture;
        Texture2D RtViewPortTexture1;
        Texture2D RtViewPortTexture2;

        #region RenderTarge Declaration for all batsmen

        private RenderTarget2D rndTargetBatsman1Score;
        private RenderTarget2D rndTargetBatsman1Info;
        private RenderTarget2D rndTargetBatsman2Score;
        private RenderTarget2D rndTargetBatsman2Info;
        private RenderTarget2D rndTargetBatsman3Score;
        private RenderTarget2D rndTargetBatsman3Info;
        private RenderTarget2D rndTargetBatsman4Score;
        private RenderTarget2D rndTargetBatsman4Info;
        private RenderTarget2D rndTargetBatsman5Score;
        private RenderTarget2D rndTargetBatsman5Info;
        private RenderTarget2D rndTargetBatsman6Score;
        private RenderTarget2D rndTargetBatsman6Info;
        private RenderTarget2D rndTargetBatsman7Score;
        private RenderTarget2D rndTargetBatsman7Info;
        private RenderTarget2D rndTargetBatsman8Score;
        private RenderTarget2D rndTargetBatsman8Info;
        private RenderTarget2D rndTargetBatsman9Score;
        private RenderTarget2D rndTargetBatsman9Info;
        private RenderTarget2D rndTargetBatsman10Score;
        private RenderTarget2D rndTargetBatsman10Info;
        private RenderTarget2D rndTargetBatsman11Score;
        private RenderTarget2D rndTargetBatsman11Info;

        #endregion

        private RenderTarget2D rndTargetScoreCard;
        private RenderTarget2D rndTargetSummary;

        #region RenderTarge Declaration for all bowlers
        
        private RenderTarget2D rndTargetBowler1Info;
        private RenderTarget2D rndTargetBowler1Score;
        private RenderTarget2D rndTargetBowler2Info;
        private RenderTarget2D rndTargetBowler2Score;
        private RenderTarget2D rndTargetBowler3Info;
        private RenderTarget2D rndTargetBowler3Score;
        private RenderTarget2D rndTargetBowler4Info;
        private RenderTarget2D rndTargetBowler4Score;
        private RenderTarget2D rndTargetBowler5Info;
        private RenderTarget2D rndTargetBowler5Score;
        private RenderTarget2D rndTargetBowler6Info;
        private RenderTarget2D rndTargetBowler6Score;
        private RenderTarget2D rndTargetBowler7Info;
        private RenderTarget2D rndTargetBowler7Score;
        private RenderTarget2D rndTargetBowler8Info;
        private RenderTarget2D rndTargetBowler8Score;
        private RenderTarget2D rndTargetBowler9Info;
        private RenderTarget2D rndTargetBowler9Score;
        private RenderTarget2D rndTargetBowler10Info;
        private RenderTarget2D rndTargetBowler10Score;
        private RenderTarget2D rndTargetBowler11Info;
        private RenderTarget2D rndTargetBowler11Score;

        #endregion


        private List<cPlayer> BatsmenOnCrease = new List<cPlayer>(2);
        private List<cPlayer> CurrentBowlers = new List<cPlayer>(2);

        private const short INFOBARSPEED = 95;
        private const short RIGHTSIDEBARSPEED = 10;
        private const short INFOBARMESSAGEINTERVAL = 7;

        //For DoubleClick and SingleClick
        private bool DblClickTimeBomb = false;
        private TimeSpan DblClickElapsedTimer = TimeSpan.Zero;
        bool click1 = false;
        const double DoubleClickDelay = 800;

        private short SubScreenState = 0;

        //For Dynamic Message store usage
        private TimeSpan elapsedTime = TimeSpan.Zero;
        private string sInfoBarMessage = string.Empty;
        private short idx = 0;

        private TimeSpan TransitionelapsedTime = TimeSpan.Zero;
        private TimeSpan PhotoFramesAnimationElapsedTime = TimeSpan.Zero;
        bool TransitionTimeBomb = false;
        bool PhotoFrameAnimationTimeBomb = false;
        bool TextPrintedOnTexture = false;
        bool DisplayRemainingBatsMan = false;
        bool DisplayRemainingBowler = false;
        bool RendereredGroupToBatsmen = false;
        bool RendereredGroupToBowlers = false;
        const float FirstColFinalXPos = 15f;
        const float SecondColFinalXPos = 0f;
        const float ThirdColFinalXPos = -15f;
        float FirstColCurrentXPos = -30;
        float SecondColCurrentXPos = -30;
        float ThirdColCurrentXPos = -30;

        #region Base Textures Declaration for 2D Gameplay Screen

        private Texture2D backgroundTexture;
        private Texture2D leftSidebarTexture;
        private Texture2D infobarTexture;
        private Texture2D leftEnclosureTexture;
        private Texture2D scorebarEnclosureTexture;
        private Texture2D scorebarBaseTexture;
        private Texture2D scorebarTexturedTexture;

        #endregion

        //3D Photoframes
        private List<PhotoFrameModel> photoFrames = new List<PhotoFrameModel>();
        #region Texture Declaration for all photoframes and photoframe meshes
        
        private Texture2D texPFBattingTeamPlayer1Info;
        private Texture2D texPFBattingTeamPlayer2Info;
        private Texture2D texPFBattingTeamPlayer3Info;
        private Texture2D texPFBattingTeamPlayer4Info;
        private Texture2D texPFBattingTeamPlayer5Info;
        private Texture2D texPFBattingTeamPlayer6Info;
        private Texture2D texPFBattingTeamPlayer7Info;
        private Texture2D texPFBattingTeamPlayer8Info;
        private Texture2D texPFBattingTeamPlayer9Info;
        private Texture2D texPFBattingTeamPlayer10Info;
        private Texture2D texPFBattingTeamPlayer11Info;
        private Texture2D texPFBowlingTeamPlayer1Info;
        private Texture2D texPFBowlingTeamPlayer2Info;
        private Texture2D texPFBowlingTeamPlayer3Info;
        private Texture2D texPFBowlingTeamPlayer4Info;
        private Texture2D texPFBowlingTeamPlayer5Info;
        private Texture2D texPFBowlingTeamPlayer6Info;
        private Texture2D texPFBowlingTeamPlayer7Info;
        private Texture2D texPFBowlingTeamPlayer8Info;
        private Texture2D texPFBowlingTeamPlayer9Info;
        private Texture2D texPFBowlingTeamPlayer10Info;
        private Texture2D texPFBowlingTeamPlayer11Info;
        //private Texture2D texPFBattingTeamPlayer1photo;
        //private Texture2D texPFBattingTeamPlayer2photo;
        //private Texture2D texPFBattingTeamPlayer3photo;
        //private Texture2D texPFBattingTeamPlayer4photo;
        //private Texture2D texPFBattingTeamPlayer5photo;
        //private Texture2D texPFBattingTeamPlayer6photo;
        //private Texture2D texPFBattingTeamPlayer7photo;
        //private Texture2D texPFBattingTeamPlayer8photo;
        //private Texture2D texPFBattingTeamPlayer9photo;
        //private Texture2D texPFBattingTeamPlayer10photo;
        //private Texture2D texPFBattingTeamPlayer11photo;
        //private Texture2D texPFBattingTeamPlayer12photo; //For 12th man
        //private Texture2D texPFBowlingTeamPlayer1photo;
        //private Texture2D texPFBowlingTeamPlayer2photo;
        //private Texture2D texPFBowlingTeamPlayer3photo;
        //private Texture2D texPFBowlingTeamPlayer4photo;
        //private Texture2D texPFBowlingTeamPlayer5photo;
        //private Texture2D texPFBowlingTeamPlayer6photo;
        //private Texture2D texPFBowlingTeamPlayer7photo;
        //private Texture2D texPFBowlingTeamPlayer8photo;
        //private Texture2D texPFBowlingTeamPlayer9photo;
        //private Texture2D texPFBowlingTeamPlayer10photo;
        //private Texture2D texPFBowlingTeamPlayer11photo;
        //private Texture2D texPFBowlingTeamPlayer12photo; //For 12th Man
        private Texture2D texPFBattingTeamPlayer1Score;
        private Texture2D texPFBattingTeamPlayer2Score;
        private Texture2D texPFBattingTeamPlayer3Score;
        private Texture2D texPFBattingTeamPlayer4Score;
        private Texture2D texPFBattingTeamPlayer5Score;
        private Texture2D texPFBattingTeamPlayer6Score;
        private Texture2D texPFBattingTeamPlayer7Score;
        private Texture2D texPFBattingTeamPlayer8Score;
        private Texture2D texPFBattingTeamPlayer9Score;
        private Texture2D texPFBattingTeamPlayer10Score;
        private Texture2D texPFBattingTeamPlayer11Score;
        private Texture2D texPFBowlingTeamPlayer1Score;
        private Texture2D texPFBowlingTeamPlayer2Score;
        private Texture2D texPFBowlingTeamPlayer3Score;
        private Texture2D texPFBowlingTeamPlayer4Score;
        private Texture2D texPFBowlingTeamPlayer5Score;
        private Texture2D texPFBowlingTeamPlayer6Score;
        private Texture2D texPFBowlingTeamPlayer7Score;
        private Texture2D texPFBowlingTeamPlayer8Score;
        private Texture2D texPFBowlingTeamPlayer9Score;
        private Texture2D texPFBowlingTeamPlayer10Score;
        private Texture2D texPFBowlingTeamPlayer11Score;

        #endregion

        //Team logo model
        private TeamLogoModel Team1Logo;
        private TeamLogoModel Team2Logo;
        private Texture2D Team1LogoTexture;
        private Texture2D Team1LogoNormal;
        private Texture2D Team2LogoTexture;
        private Texture2D Team2LogoNormal;
        private Texture2D Team1LogoSpec;
        private Texture2D Team2LogoSpec;

        //RightSide bar model
        private RightSideBarModel rightSideBarModel;
        private Texture2D TextureScreen;
        private Texture2D TextureScreenNormal;
        private Texture2D TextureScreenSpec;
        private Texture2D TextureSummaryScreen;
        private Texture2D TextureScoreCardScreen;
        private Texture2D TextureRightSideBarHeightMap;

        //Infobar and Right Sidebar sliding effects variables
        private int iInfoBarStartwidth;
        private int iInfobarEndWidth;
        private int iInfobarCurrentwidth;
        private int iRightSidebarStartheight;
        private int iRightSidebarEndheight;
        private int iRightSidebarCurrentheight;
        private int iLeftEnclosureStartPosition;
        private int iLeftEnclosureEndPosition;
        private int iLeftEnclosureCurrentPosition;
        private bool bStartSidebarShrinkingTransition;
        private bool bStartSidebarExpandTransition;
        private bool bStartInfobarExpandTransition;
        private bool bStartInfobarShrinkTransition;
        private bool bInfobarExpanded;

        private float pauseAlpha;

        #endregion

        /// <summary>
        /// Load Textures directly from file.
        /// </summary>
        /// <param name="path">The complete path to the texture</param>
        /// <returns></returns>
        private Texture2D LoadTextureFromFile(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            Texture2D t2d = Texture2D.FromStream(sceneEngine.GraphicsDevice, fs);
            fs.Close();

            //RenderTarget2D result = null;

            ////Setup a render target to hold our final texture which will have premulitplied alpha values
            //result = new RenderTarget2D(sceneEngine.GraphicsDevice, t2d.Width, t2d.Height);

            //sceneEngine.GraphicsDevice.SetRenderTarget(result);
            //sceneEngine.GraphicsDevice.Clear(Color.Black);

            ////Multiply each color by the source alpha, and write in just the color values into the final texture
            //BlendState blendColor = new BlendState();
            //blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;

            //blendColor.AlphaDestinationBlend = Blend.Zero;
            //blendColor.ColorDestinationBlend = Blend.Zero;

            //blendColor.AlphaSourceBlend = Blend.SourceAlpha;
            //blendColor.ColorSourceBlend = Blend.SourceAlpha;

            //SpriteBatch spriteBatch = new SpriteBatch(sceneEngine.GraphicsDevice);
            //spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
            //spriteBatch.Draw(t2d, t2d.Bounds, Color.White);
            //spriteBatch.End();

            ////Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
            //BlendState blendAlpha = new BlendState();
            //blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;

            //blendAlpha.AlphaDestinationBlend = Blend.Zero;
            //blendAlpha.ColorDestinationBlend = Blend.Zero;

            //blendAlpha.AlphaSourceBlend = Blend.One;
            //blendAlpha.ColorSourceBlend = Blend.One;

            //spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
            //spriteBatch.Draw(t2d, t2d.Bounds, Color.White);
            //spriteBatch.End();

            ////Release the GPU back to drawing to the screen
            //sceneEngine.GraphicsDevice.SetRenderTarget(null);

            return t2d as Texture2D;
        }

    }
}